package View;

import Service.CustomerList;
import bean.Customer;

import java.util.Scanner;

/**
 * @author caesar
 * @date 2025/7/8
 * @Description:
 * @apiNote
 */
public class CustomerView {
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        boolean flag = true;
        while (flag) {
            System.out.println("-----------------拼电商客户管理系统-----------------");
            System.out.println("\t\t\t\t1 添 加 客 户");
            System.out.println("\t\t\t\t2 修 改 客 户");
            System.out.println("\t\t\t\t3 删 除 客 户");
            System.out.println("\t\t\t\t4 客 户 列 表");
            System.out.println("\t\t\t\t5 退    出");
            System.out.print("\t\t\t\t请选择(1-5)：");
            int select = input.nextInt();
            switch (select) {
                case 1 -> add();
                case 2 -> update();
                case 3 -> delete();
                case 4 -> list();
                case 5 -> {
                    flag = false;
                    input.close();
                }
            }
        }
    }

    private static void add() {//添加
        System.out.println("---------------------添加客户---------------------");
        System.out.print("姓名：");
        String name = input.next();

        System.out.print("性别：");
        char gender = input.next().charAt(0);

        System.out.print("年龄：");
        int age = input.nextInt();

        System.out.print("电话：");
        String phone = input.next();

        System.out.print("邮箱：");
        String email = input.next();

        //（1）先装到一个Customer对象中
        Customer customer = new Customer(name, gender, age, phone, email);
        //（2）再装到Customer[]数组中
        CustomerList.addCustomer(customer);

        System.out.println("---------------------添加完成---------------------");
    }

    private static void delete() {//删除
        System.out.println("---------------------删除客户---------------------");
        System.out.print("请选择待删除客户编号(-1退出)：");
        int id = input.nextInt();
        if (id == -1) {
            return;
        }
        System.out.print("确认是否删除(Y/N)：");
        char confirm = input.next().charAt(0);
        if (confirm == 'Y' || confirm == 'y') {
            CustomerList.deleteCustomer(id);
        }
        System.out.println("---------------------删除完成---------------------");
    }

    private static void update() {//修改
        System.out.println("---------------------修改客户---------------------");
        System.out.print("请选择待修改客户编号(-1退出)：");
        int id = input.nextInt();
        if (id == -1) {
            return;
        }

        //调用CustomerList的getCustomer方法，获取到id对应的客户对象
        Customer customer = CustomerList.getCustomer(id);

        input.nextLine();//用于读取编号后面的回车换行符

        System.out.print("姓名(" + customer.getName() + ")：");
        String name = input.nextLine();
        //此处表达的意思是，如果用户没有输入，输入的是回车换行，读取为空，则使用原来的值
        if ("".equals(name)) {//这两种表达形式都可以，都表示判断空字串
            //if(name.length<=0)
            name = customer.getName();
        }

        System.out.print("性别(" + customer.getGender() + ")：");
        String sex = input.nextLine();
        char gender;
        if ("".equals(sex)) {
            gender = customer.getGender();
        } else {
            gender = sex.charAt(0);
        }

        System.out.print("年龄(" + customer.getAge() + ")：");
        String ageStr = input.nextLine();
        int age;
        if ("".equals(ageStr)) {
            age = customer.getAge();
        } else {
            //有一丢丢超纲的内容
            age = Integer.parseInt(ageStr);
        }

        System.out.print("电话(" + customer.getPhone() + ")：");
        String tel = input.nextLine();
        if ("".equals(tel)) {
            tel = customer.getPhone();
        }

        System.out.print("邮箱(" + customer.getEmail() + ")：");
        String email = input.nextLine();
        if ("".equals(email)) {
            email = customer.getEmail();
        }

        Customer newCustomer = new Customer(name, gender, age, tel, email);

        //替换原来对象
        CustomerList.replaceCustomer(id - 1, newCustomer);

        System.out.println("---------------------修改完成---------------------");
    }

    private static void list() {//列表
        System.out.println("---------------------------客户列表---------------------------");
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
        Customer[] customers = CustomerList.getAllCustomers();
        for (int i = 0; i < customers.length; i++) {
            System.out.println((i + 1) + "\t" + customers[i]);//当打印对象或字符串与对象拼接时，就会自动调用对象的toString方法
        }
        System.out.println("---------------------------客户列表完成---------------------------");
    }

}
